#!/usr/bin/ent python

import os, json, csv

# use gunzip to uncompressed gz file
def uncompressed_file(file_name):
    if os.path.exists(file_name):
        return True
    
    compressed_file_suffix = ".gz"
    if os.path.exists(file_name+compressed_file_suffix):
        command = "gunzip -c " + file_name + compressed_file_suffix + " > " + file_name
        os.system(command)
        return True
    else:
        return False


# extract job events from csv file, 
# only save job id and user hash value.
# return a list
def extract_job_events(file_name):
    result = []
    if not uncompressed_file(file_name):
        return result
    
    with open(file_name) as csv_file:
        data_reader = csv.reader(csv_file)
        for row in data_reader:
            # row[2] is job id
            # row[4] is user hash value
            # row[5] is scheduling class
            result.append((row[2], row[4], row[5]))

    delete_uncompressed_file(file_name)    
    return result


# extract task usage from csv file, 
# only save job_id, task_id, start time and end time
# return a list
def extract_task_usage(file_name):
    result = []
    if not uncompressed_file(file_name):
        return result
       
    with open(file_name) as csv_file:
        data_reader = csv.reader(csv_file)
        for row in data_reader:
            # row[2] is job id
            # row[3] is task_id
            # row[0] is start time
            # row[1] is end time
            result.append((row[2], row[3], row[0][0:-6], row[1][0:-6]))

    delete_uncompressed_file(file_name)
    return result


# extract task events from csv file,
# only save user hash value, job id and priority
# return a list
def extract_task_events(file_name):
    result = []
    if not uncompressed_file(file_name):
        return result
       
    with open(file_name) as csv_file:
        data_reader = csv.reader(csv_file)
        for row in data_reader:
            # row[6] us user hash value
            # row[2] is job id
            # row[7] is scheduling class
            # row[8] is priority
            result.append((row[6], row[2], row[7], row[8]))

    delete_uncompressed_file(file_name)
    return result


# given a file that contains a dict
# sort it to a list and return
def sort_dict_in_file(file_name):
    if not os.path.exists(file_name):
        return
    
    with open(file_name, "r") as f:
        records = json.load(f)
        keys = records.keys()
        result = [(int(k), records[k]) for k in keys]
        result.sort()
    
    with open(file_name, "w") as f:
        for (index, value) in result:
            content = str(index)+" "+str(value)
            f.write(content+"\n")


# delete given file
def delete_uncompressed_file(file_name):
	os.system("rm "+file_name)